/* Setu JS default file */
$(document).keyup(function(e) {
    if (e.keyCode == 27)
    {
        if ($('#dialog_overlay:visible').length > 0)
        {
            hideDialog();
        }
    }
});

function init()
{
    headerButtons();
    if ($('#dialog').hasClass('dialog_on'))
    {
        showDialog();
    }
}

function adjustHeights()
{
    var menuH = $('#left_menu_content').outerHeight();
    var mainH = $('#page_content').outerHeight();
    if (menuH > mainH)
    {
        $('#page_content').css('min-height', menuH+'px');
    }
    else
    {
        $('#left_menu_content').height(mainH);
    }
}

function showDialog()
{
    $("#dialog").show();
    $('#dialog_overlay').show().height($('body').height()+16 /* Body default margin */);
    $('#dialog_title_text').css('line-height', $('#dialog_title_text').height()+'px');
    $('#dialog_title_close').css('line-height', $('#dialog_title_close').height()+'px');
    $('#dialog_title_close').unbind('click').bind('click', function(){ hideDialog(); });
    setTimeout(function(){ hideDialog();}, 10000);
}

function hideDialog()
{
    $("#dialog").hide(); $('#dialog_overlay').hide();
}

function headerButtons()
{
    $('#login_input_button').button({icons: {primary: "ui-icon-comment"}}).bind('click', function(){$('#header_login_form').submit();});
    $('#home_button').button({icons: {primary: "ui-icon-home"}});
    $('#register_button').button({icons: {primary: "ui-icon-person"}});
    $('#contact_button').button({icons: {primary: "ui-icon-mail-closed"}});
    $('#favs_button').button({icons: {primary: "ui-icon-star"}});
    $('#help_button').button({icons: {primary: "ui-icon-help"}});
    $('#about_button').button({icons: {primary: "ui-icon-suitcase"}});
    $('#who_button').button({icons: {primary: "ui-icon-lightbulb"}});
    $('#logout_input_button').button({icons: {primary: "ui-icon-close"}});
    $('#configuration_button').button({icons: {primary: "ui-icon-wrench"}});
}

function notice(msg)
{
    $('#dialog_title').removeClass('dialog_error');
    $('#dialog_title_text').html('AVISO');
    $('#dialog_image').html('<img src="/images/core/notice.png"/></div>');
    $('#dialog_content').html('<li class="dialog_line">'+msg+'</li>');
    showDialog();
}

function error(msg)
{
    $('#dialog_title').addClass('dialog_error');
    $('#dialog_title_text').html('ERROR');
    $('#dialog_image').html('<img src="/images/core/error.png"/></div>');
    $('#dialog_content').html('<li class="dialog_line error">'+msg+'</li>');
    showDialog();
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

// Make buttons
$(function()
{
    $('.make_submit_button').each(function(){
        var o   = $(this);
        var val = o.val();
        var btn = $('<span class="submit_button_automatic___">'+val+'</span>');
        btn.bind('click', function() { o.trigger('click') } )
           .button({icons: {primary: "ui-icon-disk"}});
        o.hide();
        o.after(btn);
    });
});

// Get formatted time from seconds
function getTimeFromSeconds(value)
{
    var hours     = Math.floor(value/(60*60));
    var remaining = value - (hours*60*60);
    var minutes   = Math.floor(remaining/60);
    remaining     = remaining - (minutes*60);
    hours   = (hours > 9)?hours:'0'+hours;
    minutes = (minutes > 9)?minutes:'0'+minutes;
    seconds = (remaining > 9)?remaining:'0'+remaining;
    return hours+':'+minutes+':'+seconds;
}

// Spinner time config
$.widget( "ui.timespinner", $.ui.spinner, {
    options: {
        aux  : '',
        create : function(event, ui)
        {
            var o = $(this);
            var name = o.attr('name');
            var val = o.timespinner('value');
            o.removeAttr('name');
            var hidden = $('<input type="hidden" name="'+name+'" value="'+val+'" />');
            o.after(hidden);
            o.timespinner('option', 'aux', hidden);
        },
        stop: function( event, ui )
        {
            var o   = $(this);
            var aux = o.timespinner('option', 'aux');
            aux.val(o.timespinner('value'));
        }
    },
    _parse: function( value )
    {
        if ( typeof value === "string" )
        {
            var split   = (value.split(':'));
            if (split.length > 1)
            {
                var seconds = (split[0]*60*60)+(split[1]*60)+(split[2]*1);
                return seconds;
            }
            else
            {
                return value;
            }
        }
        else
        {
            return value;
        }
    },
    _format: function( value )
    {
        return getTimeFromSeconds(value);
    }
});

function onlyNumbers(elmId, initialize, fillWithZero){
	$('#'+elmId).unbind('keypress').bind('keypress', function(e) {
		return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
	});
	if(initialize != undefined)
	{
		if(initialize.length == 0)
		{
			if(fillWithZero != undefined && fillWithZero == true)
				initialize = "0";
			else
				initialize = "";
		}
		$('#' + elmId).val(initialize);
	}
}


function onlyFloatNumbers(elmId) {
	$('#'+elmId).unbind('keypress').bind('keypress', function(e) {
		return onlyFloatByObj($(this), e.which);
	});
}

function onlyPatente(elmId)
{
    $('#'+elmId).unbind('keydown').bind('keydown', function(e)
    {
        var event = e;
        var key = event.keyCode || event.charCode;
        event.keyCode = key;
        var len = $('#'+elmId).val().length;
        if (e.keyCode === 37 || e.keyCode === 39 || e.keyCode === 16)
        {
            var val = $('#'+elmId).val();
            $('#'+elmId).focus();
            $('#'+elmId).val('');
            $('#'+elmId).val(val);
            return false;
        }
        else if (e.keyCode === 8)
        {
            if (len == 4)
            {
                $('#'+elmId).val($('#'+elmId).val().substr(0,2));
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            if (len <=2)
            {
                var ch = String.fromCharCode(e.keyCode).toUpperCase();
                var chCode = ch.charCodeAt(0);
                if ((chCode>=65 && chCode<=90))
                {
                    $('#'+elmId).val($('#'+elmId).val()+''+ch);
                    if (len == 2)
                    {
                        $('#'+elmId).val($('#'+elmId).val()+'-');
                    }
                }
                return false;
            }
            else if ((len>=4) && (len <= 6))
            {
                return (e.keyCode >= 48 && e.keyCode <= 57);
            }
            else
            {
                return false;
            }
        }
	});
}

function setAccion(accion, permiso)
{
    if (!permiso)
    {
        $('.accion_'+accion).hide();
    }
}

function hideAcciones()
{
    $('.menu_parent').each(function(){
        var childrenVisible = $(this).children('ul').children('li:visible').length;
        if (childrenVisible == 0)
        {
            $(this).hide();
        }
    });
}